package com.larno.larnosqliteexample.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v4.widget.SimpleCursorAdapter;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.larno.larnosqliteexample.R;
import com.larno.larnosqliteexample.database.dao.TodoDao;
import com.larno.larnosqliteexample.database.provider.TodoContentProvider;
import com.larno.larnosqliteexample.database.tables.TodoTable;

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener, LoaderManager.LoaderCallbacks<Cursor>, AdapterView.OnItemLongClickListener {

    private ListView listView;
    private static final int LOADER_TODO = 1;
    private SimpleCursorAdapter simpleCursorAdapter;
    private TextView textTips;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(MainActivity.this, DetailActivity.class));
            }
        });

        textTips = (TextView) findViewById(R.id.textTips);
        listView = (ListView) findViewById(R.id.listView);
        listView.setOnItemClickListener(this);
        listView.setOnItemLongClickListener(this);

        String[] from = new String[]{TodoTable.COLUMN_CATEGORY, TodoTable.COLUMN_SUMMARY, TodoTable.COLUMN_DESCRIPTION};
        int[] to = new int[]{R.id.textCategory, R.id.textSummary, R.id.textDescription};
        getSupportLoaderManager().initLoader(LOADER_TODO, null, this);
        simpleCursorAdapter = new SimpleCursorAdapter(this, R.layout.row_todo, null, from, to, 0);
        listView.setAdapter(simpleCursorAdapter);


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        showTodoDetail(id);
    }

    private void showTodoDetail(long id) {
        Intent i = new Intent(this, DetailActivity.class);
        Uri todoUri = Uri.parse(TodoContentProvider.CONTENT_URI + "/" + id);
        i.putExtra(TodoContentProvider.CONTENT_ITEM_TYPE, todoUri);
        startActivity(i);
    }

    @Override
    public boolean onItemLongClick(final AdapterView<?> parent, final View view, int position, final long id) {

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setCancelable(true);
        builder.setTitle(R.string.opertion);
        builder.setItems(R.array.operations, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                switch (which){
                    case 1:
                        deleteTodo(id);
                        break;
                    case 2:
                        showTodoDetail(id);
                        break;
                }
            }
        });
        return true;
    }

    private void deleteTodo(long id) {
        Uri todoUri = Uri.parse(TodoContentProvider.CONTENT_URI + "/" + id);
        int rowsDeleted = TodoDao.delete(MainActivity.this, todoUri);
        Snackbar.make(listView, String.valueOf(rowsDeleted),Snackbar.LENGTH_LONG).show();
    }

    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        CursorLoader cursorLoader = null;
        switch (id){
            case LOADER_TODO:
                String[] projection = {TodoTable.COLUMN_ID,TodoTable.COLUMN_CATEGORY, TodoTable.COLUMN_SUMMARY, TodoTable.COLUMN_DESCRIPTION};
                cursorLoader = new CursorLoader(this, TodoContentProvider.CONTENT_URI, projection, null, null, null);
                break;
        }
        return cursorLoader;
    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
        simpleCursorAdapter.swapCursor(data);
        if(data != null && data.getCount() > 0){
            textTips.setVisibility(View.GONE);
        }
    }

    @Override
    public void onLoaderReset(Loader<Cursor> loader) {
        simpleCursorAdapter.swapCursor(null);
    }

}
